//
//  AddGuanLiYuanViewController.m
//  ShouKuang
//
//  Created by toro宇 on 2019/5/31.
//  Copyright © 2019 CodeYY. All rights reserved.
//

#import "AddGuanLiYuanViewController.h"
#import "AlertOperationView.h"
@interface AddGuanLiYuanViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)UIView *searchView;
@property (nonatomic, strong)NSArray *dataAry;
@property (nonatomic, assign)AddType addType;

@end
@implementation AddGuanLiYuanViewController
+(AddGuanLiYuanViewController *)addGuanLiYuanViewController:(AddType)addType
{
    AddGuanLiYuanViewController *vc = [AddGuanLiYuanViewController new];
    vc.addType = addType;
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataAry = @[@[@"财务部",@"财务部"],@[@"佛挡杀佛",@"对方是否",@"对方是否",@"对方是否",@"佛挡杀佛"]];
    [self initNavBar];
    [self initCustomUI];
    
    
    // Do any additional setup after loading the view.
}

-(void)initNavBar
{
    WEAKSELF
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"nav_back_red"] forState:UIControlStateNormal];
    [leftBtn setTitle:@"返回" forState:UIControlStateNormal];
    [leftBtn setTitleColor:YYRGB(129, 129, 129) forState:UIControlStateNormal];
    [leftBtn jk_setImagePosition:LXMImagePositionLeft spacing:5];
    [[leftBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    if (self.addType == GuanLiAddType) {
        self.title = @"添加管理员";
    }else if (self.addType == ShouYiAddType){
        self.title = @"添加收银员";
    }
}
- (void)initCustomUI
{
    __weak typeof(self) weakSelf = self;
    [self initSearchView];
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(weakSelf.searchView.mas_bottom);
    }];
    _tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCells"];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,Main_Screen_Width, 40)];
    tableView.tableHeaderView = headView;
    UILabel *leftLab = [UILabel new];
    [headView addSubview:leftLab];
    [leftLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(16);
        make.centerY.offset(0);
    }];
    leftLab.textColor = [UIColor redColor];
    leftLab.text = @"联系人";
    
    UIImageView *imgView = [UIImageView new];
    [headView addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftLab.mas_right).offset(16);
        make.centerY.offset(0);
    }];
    imgView.image = [UIImage imageNamed:@""];
    
    UILabel *rightLab = [UILabel new];
    [headView addSubview:rightLab];
    [rightLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imgView.mas_right).offset(16);
        make.centerY.offset(0);
    }];
    rightLab.text = @"深圳中数信科技有限公司";
}

-(void)initSearchView
{
    __weak typeof(self) weakSelf = self;
        UIView *contentView = [UIView new];
        contentView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:contentView];
    _searchView = contentView;
        [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.offset(0);
            make.top.offset(70);
            make.height.mas_equalTo(60);
        }];
        UITextField *textField = [[UITextField alloc] init];
        
        // 搜索按钮
        UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [contentView addSubview:searchBtn];
        [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-20);
            make.centerY.offset(0);
            make.size.mas_equalTo(CGSizeMake(70, 30));
        }];
        [searchBtn setTitle:@"搜索" forState:(UIControlStateNormal)];
        searchBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        searchBtn.layer.cornerRadius = 8;
        searchBtn.layer.masksToBounds = YES;
        searchBtn.backgroundColor = YYRGB(219, 67, 57);
        [[searchBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            NSString *searchStr = textField.text;
            if (!searchStr.length) {
                [weakSelf.view makeToast:@"查询内容不可为空"];
                return;
                
            }
            [weakSelf.view makeToast:@"正在查询请稍等"];
            [weakSelf.tableView reloadData];
        }];
        
        // 搜索框
        [contentView addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.centerY.offset(0);
            make.right.equalTo(searchBtn.mas_left).offset(-12);
            make.height.mas_equalTo(33);
        }];
        textField.layer.masksToBounds = YES;
        textField.layer.cornerRadius = 8;
        textField.layer.borderColor = YYRGB(218, 218, 218).CGColor;
        textField.layer.borderWidth = 1;
        textField.backgroundColor = YYRGB(244, 244, 244);
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(8, 2, 16, 16)];
        UIView *bg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 32, 20)];
        [bg addSubview:imageView];
        imageView.image = [UIImage imageNamed:@"search"];
        
        UIView *line = [UIView new];
        [bg addSubview:line];
        line.backgroundColor =  YYRGB(218, 218, 218);
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(imageView);
            make.left.equalTo(imageView.mas_right).offset(5);
            make.size.mas_equalTo(CGSizeMake(1, 15));
        }];
        
        textField.leftView = bg;
        textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.leftViewMode = UITextFieldViewModeAlways;
}


#pragma mark -delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return [self.dataAry[section] count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataAry.count;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCells" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    
    if (indexPath.section == 0) {
        
        UILabel *titleLab = [UILabel new];
        [cell addSubview:titleLab];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.centerY.offset(0);
        }];
        titleLab.textColor = [UIColor darkGrayColor];
        titleLab.text = self.dataAry[indexPath.section][indexPath.row];
        
        UIImageView *img = [UIImageView new];
        [cell addSubview:img];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-16);
            make.centerY.offset(0);
//            make.size.mas_equalTo(CGSizeMake(25, 25));
        }];
        img.image = [UIImage imageNamed:@"order_revoke"];
      
        
    }else{
        UIImageView *img = [UIImageView new];
        [cell addSubview:img];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.centerY.offset(0);
            make.size.mas_equalTo(CGSizeMake(25, 25));
        }];
        img.layer.cornerRadius = 25/2;
        img.layer.masksToBounds = YES;
        
        UILabel *titleLab = [UILabel new];
        [cell addSubview:titleLab];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(img.mas_right).offset(10);
            make.centerY.offset(0);
        }];
        titleLab.textColor = [UIColor lightGrayColor];
        
        UILabel *contentLab = [UILabel new];
        [cell addSubview:contentLab];
        [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.offset(-16);
            make.centerY.offset(0);
        }];
        contentLab.textColor = [UIColor darkGrayColor];
        
        // 赋值
        img.backgroundColor = [UIColor greenColor];
        titleLab.text = self.dataAry[indexPath.section][indexPath.row];
        contentLab.text= @"超级管理员";
    }
    
    
    
    
    UIView *line  = [UIView new];
    [cell addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.mas_equalTo(1);
    }];
    line.backgroundColor = [UIColor darkGrayColor];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.row) {
        case 0:
            [AlertOperationView alertOperationView:@"success_red" titleStr:@"设置成功" sureBtnBlock:nil cancleBtnBlock:nil];
            break;
        case 1:
            [AlertOperationView alertOperationView:nil titleStr:@"该商户号管理员已存在,无需重复添加" sureBtnBlock:^(){
                
            } cancleBtnBlock:nil];

            break;
        case 2:
            [AlertOperationView alertOperationView:nil titleStr:@"该用户已是其他商户号收银员,申请调换成本商户收银员请联系原商户号管理员" sureBtnBlock:^(){
                
            } cancleBtnBlock:nil];

            break;
        case 3:
            [AlertOperationView alertOperationView:nil titleStr:@"是否将KVV设为模块管理员" sureBtnBlock:^(){
                
                [AlertOperationView alertOperationView:@"success_red" titleStr:@"设置成功" sureBtnBlock:nil cancleBtnBlock:nil];

            } cancleBtnBlock:^(){
                
            }];

            break;
        case 4:
            [AlertOperationView alertOperationView:nil titleStr:@"是否将KVV设为收银院内" sureBtnBlock:^(){
                [AlertOperationView alertOperationView:@"success_red" titleStr:@"设置成功" sureBtnBlock:nil cancleBtnBlock:nil];

            } cancleBtnBlock:^(){
                
            }];
            break;
        default:
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 50;
    }else{
        return 60;

    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
-(NSArray *)dataAry
{
    if (!_dataAry) {
        _dataAry = [NSArray array];
    }
    return _dataAry;
}
@end
